package com.hxexp.weixin.test;

import java.io.IOException;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.fasterxml.jackson.core.type.TypeReference;
import com.hxexp.weixin.common.config.HpeApiURLConfig;
import com.hxexp.weixin.common.exception.APIException;
import com.hxexp.weixin.common.httpclient.Json2ObjectHandler;
import com.hxexp.weixin.common.json.JsonUtil;
import com.hxexp.weixin.hpe.api.returnamount.model.ReturnAmountParamBean;
import com.hxexp.weixin.hpe.api.returnamount.model.ReturnAmountResultBean;
import com.hxexp.weixin.test.runner.JUnit4WithSpringLogClassRunner;

/**
 * HttpClient自定义响应处理器测试
 *
 * @author wang.bin
 *
 */
@RunWith(JUnit4WithSpringLogClassRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ContextConfiguration(locations = { "classpath:conf/spring/spring-biz.xml", "classpath:conf/spring/spring-ehcache.xml" })
public class Json2ObjectHandlerTester {

    private final CloseableHttpClient httpclient = HttpClients.createDefault();
    private static final String URI = "ticket/returnamount";
    @Autowired
    private HpeApiURLConfig config;
    @Test
    public void testCase01() throws APIException {

        ReturnAmountParamBean returnAmountParamBean = new ReturnAmountParamBean();
        returnAmountParamBean.setTicketId("22564744");

        ReturnAmountResultBean expected = new ReturnAmountResultBean();
        ReturnAmountResultBean returnValue = new ReturnAmountResultBean();

        expected.setErrcode(0);
        expected.setErrmsg("");
        expected.setReturnAmount("0.00");
        expected.setTotalReturnAmount(null);

        HttpPost post = new HttpPost(config.getURL(URI));
        String postData = JsonUtil.writeObject2JsonString(returnAmountParamBean);

        // 设置请求体数据
        StringEntity entity = new StringEntity(postData, ContentType.create("application/json", "utf-8"));
        post.setEntity(entity);

        TypeReference<ReturnAmountResultBean> typeReference = new TypeReference<ReturnAmountResultBean>() {
        };
        try {
            returnValue = httpclient.execute(post, new Json2ObjectHandler<ReturnAmountResultBean>(typeReference));
        } catch (IOException e) {
            throw new APIException(e);
        }

        Assert.assertEquals(expected.getErrcode(), returnValue.getErrcode());
        Assert.assertEquals(expected.getErrmsg(), returnValue.getErrmsg());
        Assert.assertEquals(expected.getReturnAmount(), returnValue.getReturnAmount());
        Assert.assertEquals(expected.getTotalReturnAmount(), returnValue.getTotalReturnAmount());
    }
}
